<?php
/**
 * @Author: Kendy
 * @Date:    2019-11-15 09:56:41
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2019-11-15 14:08:20
 */
namespace app\common\model; 
use think\Model;
use think\Db;  
use app\common\model\Settlements;
class Settlements extends Model
{

    /**
     * [getSettledMoney 获取商家结算的金额]
     * @Author   kendy
     * @DateTime 2019-11-15T10:11:04+0800
     * @param    [type]                   $object_id   [店铺id]
     * @param    [type]                   $object_type [店铺类型1酒店，2景点，3旅行社，4导游]
     * @return   [type]                                [description]
     */
	public function getSettledMoney($object_id,$object_type)
	{
        $where = [];  
        $arr = $this->getTable($object_type);  
        $where['is_del'] = 0;
        $where['order_status'] = ['in',[3,4]];
        $where['o.'.$arr['field']] = $object_id;
        $where['settlement_status'] = 1;
    	$money = Db::name($arr['table'])
    	->alias('o')
    	->join('settlements s','o.settlement_id=s.settlement_id')
    	->where($where) 
    	->sum('back_money');  
        return $money;

      }
	/**
	 * [getNoSettleMoney 获取当月未结算的金额]
	 * @Author   kendy
	 * @DateTime 2019-11-15T11:27:36+0800
	 * @return   [type]                   [description]
	 */
      public function getNoSettleMoney($object_id,$object_type)
      {  
	      	$first_day = date('Y-m-01',time());
	        $last_day =  strtotime("$first_day +2 month -1 day");
    	    $where = [];
            //根据object_type获取不同类型的订单表名字 
            $arr = $this->getTable($object_type);  
	    	$where['is_del'] = 0;
	    	$where['order_status'] = ['in',[3,4]];
	    	$where['settlement_id'] = 0;
	    	$where['create_time'] = ['between',[$first_day,$last_day]];
	        $where[$arr['field']] = $object_id;  
	    	$order_price =  Db::name($arr['table'])
	    	->where($where)  
	        ->sum('order_price');    
	        return $order_price; 
      }
    
     /**
      * [getTable 根据object_type获取订单表信息]
      * @Author   kendy
      * @DateTime 2019-11-15T13:18:20+0800
      * @param    [type]                   $object_type [对象类型]
      * @return   [type]                                [description]
      */
      public function getTable($object_type)
      {
      	  
      	  switch ($object_type) {
	        case 1:
	            $arr['table'] = 'hotel_order';
	            $arr['field']='hotel_id';
	        case 2:
	            $arr['table'] ='tour_order';
	            $arr['field']='tour_id';
	        case 3:
	            $arr['table'] ='gtour_order';
	            $arr['field']='gtour_id';
	        case 4:
	            $arr['table'] ='guide_order';
	            $arr['field']='guide_id';
	       } 
          return $arr;
      }


 }